
chrome.storage.sync.get(['jianshu'], function(result) {
    console.log('jianshu.js: Value currently is ' + result.jianshu);
    if (result.jianshu) jianshuSimple();
});

function jianshuSimple() {
    if (!document.URL.includes("jianshu.com")) {
        return;
    }

    insertCss('jianshu/jianshu.css');

    let content = document.getElementsByTagName('section')[0]
    if(!content) {
        jianshuSpecial();
        return
    }

    let {ful_scr_toolbar,ful_scr_toolbar_close} = inject_iReaderBar()
    ful_scr_toolbar_close.onclick= onReadBtnClick;


    let bodyLastBgColor = document.body.style.backgroundColor;
    let main = document.body

    function enterFullScreen(){
        ful_scr_toolbar.className = "ful_scr_toolbar"
        ful_scr_toolbar_close.textContent = "×";

        showHeaderAndFooter(false);

        chrome.storage.sync.get(['bgcolor'], function(result) {
            let bgcolor = result.bgcolor?result.bgcolor:"rgb(227, 227, 227)";
            main.setAttribute('style', "background:"+bgcolor+" !important");
        });

        main.className += "iread_main";

        content.style = "left:0;padding:40px 12.5%"
        content.parentElement.style = "position:absolute;width:100%;padding:40px 12.5% 30px 12.5%"
        content.parentElement.parentElement.style = "width:80%;padding:0"
 
        showDonate(true);
    }

    function exitFullScreen(){
        ful_scr_toolbar.className = "ful_scr_toolbar_notful";
        ful_scr_toolbar_close.textContent = "+";

        showHeaderAndFooter(true);

        if (main) main.className = main.className.replace("iread_main", "");
        main.style.background = bodyLastBgColor;

        content.style = ""
        content.parentElement.style = ""
        content.parentElement.parentElement.style = ""
 
        showDonate(false);
    }

    function onReadBtnClick(){
        if (main && main.className.includes("iread_main")) {
            exitFullScreen();
        } else {
            enterFullScreen();
        }
    }

    function showHeaderAndFooter(show) {
        let headers = document.getElementsByTagName("header");
        let footers = document.getElementsByTagName("footer");
        let asides = document.getElementsByTagName("aside");

        if (headers&&headers[0])headers[0].style.display = show?"block":"none";
        if (footers&&footers[0])footers[0].style.display = show?"block":"none";
        if (asides&&asides[0])asides[0].style.display = show?"block":"none";
    }

    chrome.storage.sync.get(['i_jianshu'], function(result) {
        console.log('jianshu.js: auto is ' + result.i_jianshu);
        if (result.i_jianshu) enterFullScreen();
    });
}

function jianshuSpecial() {
    let bodyLastBgColor = document.body.style.backgroundColor;
    let main = document.body

    insertCss('jianshu/jianshu2.css');

    let nav = document.getElementsByTagName('nav')[0]
    let post = document.getElementsByClassName('post')[0]
    let note_bottom = document.getElementsByClassName('note-bottom')[0]
    let {ful_scr_toolbar,ful_scr_toolbar_close} = inject_iReaderBar()

    function enterFullScreen(){
        ful_scr_toolbar.className = "ful_scr_toolbar"
        ful_scr_toolbar_close.textContent = "×";

        if(post)post.style="width:75%;padding:30px 60px;background:white;border-radius:4px";
        if(nav)nav.style.display="none"
        if(note_bottom)note_bottom.style="margin:20px 12.5%;padding:30px 60px;border-radius:4px;background:white;"

        chrome.storage.sync.get(['bgcolor'], function(result) {
            let bgcolor = result.bgcolor?result.bgcolor:"rgb(227, 227, 227)";
            main.setAttribute('style', "background:"+bgcolor+" !important");
        });

        main.className += "iread_main";
    }

    function exitFullScreen(){
        ful_scr_toolbar.className = "ful_scr_toolbar_notful";
        ful_scr_toolbar_close.textContent = "+";

        if(post)post.style="width:75%;padding:0;";
        if(nav)nav.style.display = ""
        if(note_bottom)note_bottom.style = "padding:0 12.5%;background:unset"

        if (main) main.className = main.className.replace("iread_main", "");
        main.style.background = bodyLastBgColor;
    }

    ful_scr_toolbar_close.onclick = function(){
        if (main && main.className.includes("iread_main")) {
            exitFullScreen();
            showDonate(false);
        } else {
            enterFullScreen();
            showDonate(true);
        }
    }

    chrome.storage.sync.get(['i_jianshu'], function(result) {
        console.log('jianshu.js: auto is ' + result.i_jianshu);
        if (result.i_jianshu) enterFullScreen();
    });
}